package com.jyw.business.knowledge.controller;

import com.jyw.business.knowledge.domain.dto.KlKnowledgeEditPassDto;
import com.jyw.business.knowledge.domain.dto.KlKnowledgePageDto;
import com.jyw.business.knowledge.domain.dto.KlKnowledgeStateDto;
import com.jyw.business.knowledge.domain.vo.KlKnowledgeVo;
import com.jyw.business.knowledge.service.IKlKnowledgeService;
import com.jyw.common.annotation.Log;
import com.jyw.common.core.controller.BaseController;
import com.jyw.common.core.domain.CommonResult;
import com.jyw.common.core.domain.model.SimpleDto;
import com.jyw.common.core.page.TableDataInfo;
import com.jyw.common.enums.BusinessType;
import com.jyw.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 知识库-信息Controller
 *
 * @author ruoyi
 * @date 2023-11-17
 */
@RestController
@RequestMapping("/knowledge/knowledge")
@Api(tags = "知识库管理")
public class KlKnowledgeController extends BaseController {
    @Resource
    private IKlKnowledgeService klKnowledgeService;

    /**
     * 查询知识库知识审核通过列表
     */
    @GetMapping("/passList")
    @ApiOperation("查询知识库知识审核通过列表")
    public TableDataInfo<KlKnowledgeVo> passList(KlKnowledgePageDto pageDto) {
        startPage();
        Boolean isAdmin = SecurityUtils.isAdmin();
        Long userId = getUserId();
        pageDto.setUserId(userId);
        pageDto.setIsAdmin(isAdmin);
        List<KlKnowledgeVo> list = klKnowledgeService.getPassList(pageDto);
        return getDataTable(list);
    }

    /**
     * 标题和助记简拼查询知识库列表
     */
    @PostMapping(value = "/getListByKeyword")
    @ApiOperation("标题和助记简拼查询知识库列表")
    public CommonResult<List<KlKnowledgeVo>> getListByKeyword(@RequestBody SimpleDto<String> dto) {
        return CommonResult.success(klKnowledgeService.getListByKeyword(dto.getData()));
    }

    /**
     * 编辑知识库
     */
    @Log(title = "编辑知识库", businessType = BusinessType.UPDATE)
    @PutMapping(value = "edit")
    @ApiOperation("编辑知识库")
    public CommonResult edit(@RequestBody @Valid KlKnowledgeEditPassDto editDto) {
        Long userId = getUserId();
        klKnowledgeService.editPass(editDto,userId);
        return CommonResult.success();
    }

    /**
     * 知识库编辑状态
     */
    @ApiOperation("知识库编辑状态")
    @Log(title = "知识库编辑状态", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('kl:knowledge:editState')")
    @PutMapping("/editState")
    public CommonResult editState(@RequestBody @Valid KlKnowledgeStateDto stateDto) {
        klKnowledgeService.editState(stateDto);
        return CommonResult.success();
    }

    /**
     * 删除知识库-信息
     */
    @Log(title = "知识库-信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除知识库")
    public CommonResult remove(@PathVariable Long[] ids) {
        return CommonResult.success(klKnowledgeService.deleteKlKnowledgeByIds(ids));
    }
}
